﻿using System.Windows.Input;
using DSPL.Contracts;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;

namespace DSPL.Finac.ViewModel
{
    public class LocationMasterViewModel : BaseViewModel
    {
        private bool isLocationCodeFocused;
        private bool isLocationNameFocused;
        private bool isLocationTypeFocused;
        private DataTable _dtLocationType;
        private ICommand locationTypeChanged;
        private string _locationTypeName;
        private bool _isLocationTypeEnabled;

        #region Properties

        public bool IsLocationCodeFocused
        {
            get { return isLocationCodeFocused; }
            set
            {
                isLocationCodeFocused = value;
                this.OnPropertyChanged("IsLocationCodeFocused");
            }
        }

        public bool IsLocationNameFocused
        {
            get { return isLocationNameFocused; }
            set
            {
                isLocationNameFocused = value;
                this.OnPropertyChanged("IsLocationNameFocused");

            }
        }

        public bool IsLocationTypeFocused
        {
            get { return isLocationTypeFocused; }
            set
            {
                isLocationTypeFocused = value;
                this.OnPropertyChanged("IsLocationTypeFocused");

            }
        }

        public DataTable DtLocationType
        {
            get { return _dtLocationType; }
            set
            {
                _dtLocationType = value;
                this.OnPropertyChanged("DtLocationType");
            }
        }

        public string LocationTypeName
        {
            get { return _locationTypeName; }
            set { _locationTypeName = value;
            this.OnPropertyChanged("LocationTypeName");
            }
        }

        public bool IsLocationTypeEnabled
        {
            get { return _isLocationTypeEnabled; }
            set { _isLocationTypeEnabled = value;
            this.OnPropertyChanged("IsLocationTypeEnabled");
            }
        }

        #endregion

        #region Define relay commands

        public ICommand LocationTypeChanged
        {
            get
            {
                if (this.locationTypeChanged == null)
                    this.locationTypeChanged = new RelayCommand(LocationType_SelectionChanged);

                return this.locationTypeChanged;
            }
        }

        #endregion

        #region Constructor

        public LocationMasterViewModel():base()
        {
            MessageTitle = "Location Master";
        }
        #endregion
        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
       protected override void PreDataValidateExecuted(object obj)
        {
            IsLocationCodeFocused = false;
            IsLocationNameFocused = false;
            IsLocationTypeFocused = false;
            var messageService = new MessageService();
            if (CurrentRow["LocCode"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.LocationNotEmpty, MessageTitle, MessageType.InfoOK);
                IsLocationCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["LocName"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.LocationNameNotEmpty,MessageTitle, MessageType.InfoOK);             
                IsLocationNameFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["LocTypePk"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.LocationTypeNotEmpty,MessageTitle, MessageType.InfoOK);              
                IsLocationTypeFocused = true;
                this.IsValid = false;
                return;
            }
            this.IsValid = true;
        }

       private void LocationType_SelectionChanged(object obj)
       {
           string locationType = CurrentRow["LocTypePk"].ToString().Trim();
           if (CurrentRow["LocTypePk"].ToString().Trim() != string.Empty)
           {
               DataRow selectedRow = DtLocationType.Rows.Find(locationType);
               LocationTypeName = selectedRow["LoctName"].ToString().Trim();
           }
           else
           {
               LocationTypeName = string.Empty;
           }

       }

       protected override  void SetDataSourcesAndProperties()
       {
           var getData = new MasterReadService();
           DtLocationType = getData.GetLocationType();
           DataColumn[] primaryKeys = new DataColumn[1];
           primaryKeys[0] = _dtLocationType.Columns[0];
           DtLocationType.PrimaryKey = primaryKeys;
       }

       protected override void EnableDisableSpecificControls()
       {
           base.EnableDisableSpecificControls();

           if (FormMode == Modes.New)
           {
               IsLocationTypeEnabled = true;
           }
           else
           {
               IsLocationTypeEnabled = false;
           }
           
       }
    }
}